#  ██████████ ████     ████ ██     ██ ██     ██
# ░░░░░██░░░ ░██░██   ██░██░██    ░██░░██   ██
#     ░██    ░██░░██ ██ ░██░██    ░██ ░░██ ██
#     ░██    ░██ ░░███  ░██░██    ░██  ░░███
#     ░██    ░██  ░░█   ░██░██    ░██   ██░██
#     ░██    ░██   ░    ░██░██    ░██  ██ ░░██
#     ░██    ░██        ░██░░███████  ██   ░░██
#     ░░     ░░         ░░  ░░░░░░░  ░░     ░░
#
#  ▓▓▓▓▓▓▓▓▓▓
# ░▓ author ▓ xero <x@xero.style>
# ░▓ code   ▓ https://code.x-e.ro/dotfiles
# ░▓ mirror ▓ https://git.io/.files
# ░▓▓▓▓▓▓▓▓▓▓
# ░░░░░░░░░░

# shell
set -g default-command $SHELL
set -g default-shell $SHELL

# start with window 1 (instead of 0)
set -g base-index 1

# renumber windows after closing
set -g renumber-windows on

# start with pane 1
set -g pane-base-index 1

# remove esc delay
set -sg escape-time 0

# history
set -g history-limit 10000

# allow terminal scrolling
set-option -g terminal-overrides 'xterm*:smcup@:rmcup@'

# true colors
set-option -sa terminal-features ',xterm-256color:RGB'

# use vi mode
setw -g mode-keys vi
set -g status-keys vi

# pleb/ipad mode
set -g mouse on

# focusing
set-option -g focus-events on

# panes
set -g pane-border-style "fg=color0"
set -g pane-active-border-style "fg=color0"
set -g window-active-style 'bg=terminal'
set -g window-style 'bg=color234'

# status line
set -g status-justify left
set -g status-style "bg=terminal,fg=color1"
set -g status-interval 2

# messaging
set -g message-style "bg=color1,fg=color0"
set -g message-command-style "bg=color1,fg=color1"

# window mode
setw -g mode-style "bg=color1,fg=color0"

# split sytle
set -g pane-border-style "bg=color234,fg=color234"
set -g pane-active-border-style "bg=color234,fg=color234"

#resizing
setw -g aggressive-resize on

# window status
set-option -g status-position bottom
setw -g window-status-format " #[bg=color1,fg=color0,noreverse]▓░ #W "
setw -g window-status-current-format " #[bg=color6,fg=color0,noreverse]▓░ #W "

# loud or quiet?
set-option -g visual-activity on
set-option -g visual-bell off
set-option -g visual-silence off
set-window-option -g monitor-activity off
set-option -g bell-action none

# tmux clock
set -g clock-mode-color color1

# change prefix to backtick
unbind C-b
set-option -g prefix `
bind ` send-prefix

# vim style copy paste mode
unbind [
bind escape copy-mode
unbind p
bind p paste-buffer
bind-key -T copy-mode-vi 'v' send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel "xsel -i" \; send-keys enter
bind-key -T copy-mode-vi 'Y' send -X copy-pipe-and-cancel "tmux loadb -" \; send-keys enter

# splitting
unbind %
bind h split-window -v
unbind '"'
bind v split-window -h

# zoom split
unbind z
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind z if-shell "$is_vim" "send-keys ,z" "resize-pane -Z"
bind Z resize-pane -Z

# vim style commands
bind : command-prompt

# source config file
bind r source-file ~/.config/tmux/tmux.conf

# other random key-binding changes
bind x kill-pane
bind t set status
bind a set-window-option synchronize-panes \; display-message "█▓░ synchronize"

# tmux plugins
set -g @plugin 'tmux-plugins/tpm'
setenv -g TMUX_PLUGIN_MANAGER_PATH '~/.config/tmux/plugins/'

# sticky fingers
set -g @plugin 'fcsonline/tmux-thumbs'
unbind f
set -g @thumbs-key f
set -g @thumbs-contrast 1
set -g @thumbs-bg-color '#bb7744'
set -g @thumbs-fg-color '#222222'
set -g @thumbs-hint-bg-color '#685742'
set -g @thumbs-hint-fg-color '#222222'
set -g @thumbs-select-bg-color '#78824B'
set -g @thumbs-select-fg-color '#222222'
set -g @thumbs-command 'printf "{}" | yank'

# status icon
set -g @plugin 'MunifTanjim/tmux-mode-indicator'
set -g @mode_indicator_empty_prompt ' ◇ '
set -g @mode_indicator_empty_mode_style 'bg=color0,fg=color6'
set -g @mode_indicator_prefix_prompt ' ◈ '
set -g @mode_indicator_prefix_mode_style 'bg=color4,fg=color0'
set -g @mode_indicator_copy_prompt '  '
set -g @mode_indicator_copy_mode_style 'bg=color5,fg=color0'
set -g @mode_indicator_sync_prompt '   '
set -g @mode_indicator_sync_mode_style 'bg=color4,fg=color0'

# navigate and resize nvim/tmux splits
set -g @plugin 'aserowy/tmux.nvim'
set -g @tmux-nvim-navigation true
set -g @tmux-nvim-navigation-cycle true
set -g @tmux-nvim-navigation-keybinding-left 'C-h'
set -g @tmux-nvim-navigation-keybinding-down 'C-j'
set -g @tmux-nvim-navigation-keybinding-up 'C-k'
set -g @tmux-nvim-navigation-keybinding-right 'C-l'
set -g @tmux-nvim-resize true
set -g @tmux-nvim-resize-step-x 2
set -g @tmux-nvim-resize-step-y 2
set -g @tmux-nvim-resize-keybinding-left 'C-Left'
set -g @tmux-nvim-resize-keybinding-down 'C-Down'
set -g @tmux-nvim-resize-keybinding-up 'C-Up'
set -g @tmux-nvim-resize-keybinding-right 'C-Right'

# status line
set -g status-right-length 100
set -g status-right '#(~/.local/bin/tmux-status)'
set -g status-left '#{tmux_mode_indicator}'

# enable OSC 52 clipboard
set -s set-clipboard on
# Ms modifies OSC 52 clipboard handling to work with mosh, see
# https://gist.github.com/yudai/95b20e3da66df1b066531997f982b57b
set -ag terminal-overrides "vte*:XT:Ms=\\E]52;c;%p2%s\\7,xterm*:XT:Ms=\\E]52;c;%p2%s\\7"
unbind p
bind p paste-buffer -p

# clipboard menu
bind space display-popup -w 70% -h 70% -E "CM_OUTPUT_CLIP=1 clipmenu | yank"

# emoji menu
bind e display-popup -w 70% -h 70% -E "emoji"

run '~/.config/tmux/plugins/tpm/tpm'
